

$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});


function del(word, listName){
    //alert(word);
    $.post("delete.jsp",  {
        word: word,  
        listName:listName
    }, function(data2) {
        
        $.post("retrieve.jsp", {
            listName: listName
        }, function(data){
            $("#wordlist").html(data);
        });
        retrieveMyList();
    });
}
function wordClick(o, word, wordId, level) {
     //$('#k').attr('src', '/pics/k.jpg');
    for(i=0; i<1; i++)
    {
        
        $(o).css("color",  "#333");
    }
    
    var oldSrc = document.getElementById("frame"+wordId).src ;
   
   // alert(word);
    var newSrc ="";
    if (level ==0) {
        newSrc =  "http://www.nciku.com/search/all/"+ encodeURI("Try " + word);
    }
    //alert(oldSrc);
    //alert(newSrc);
    if (oldSrc != newSrc)
    {
        document.getElementById("frame"+wordId).src = newSrc;
        if ($("#frame"+wordId).css("display") != "none")
        {
            $("#"+o.id).css("color",  "red");
            return;
        }
    }
    if ($("#frame"+wordId).css("display") == "none") {
        $("#frame"+wordId).css("display", "block");
        $("#"+o.id).css("color",  "red");
    }
    else
        $("#frame"+wordId).css("display", "none"); 
//alert("adfd");
}

function retrieveGradeList(grade)
{
    
    $.get("retrieveGradeList.jsp", {
        grade:grade
    }, function(data){
        $("#gradeList").html(data); 
    });
}
function upload(o)
{
                 
    listname = $.trim($('#listname').val());
    
    
    if (listname.length==0) 
    {
        if ($.cookie('currentWordList')!=null)
            listname = $.cookie('currentWordList');
        else
            listname= (new Date()).toString().substring(0,24).replace(/\W/g, '_');
        $('#listname').val(listname);
    }
                
    if ($('#inputarea').css("display") == "none") {
        $('#inputarea').css('display', 'block');
                    
        $(o).text("Save");
            
    } else {
        $('#inputarea').css('display', 'none');
                 
        $(o).text("Upload words");
        text = $.trim($('#upload').val());
                    
        if (text.length!=0)
        {
                       
            $.post("save.jsp", 
            {
                wordList:text, 
                listName: listname,
                note: $("#note").val()
            },
            function(data2){
                $.cookie("currentListName", listname);
                retrieveMyList();
                var newString = text.replace(/\W+/g,' ');
                var words = newString.split(' ');
                var currentWord = words[0];
                $.get("retrieve.jsp", {
                    listName: listname,
                    currentWord: currentWord
                }, function(data) {
                    // alert(data);
                    $("#wordlist").html(data);
                    $('#listname').val("");
                    $('#upload').val("");
                }); 
            });
                        
                        
        }
    }
}
            
      
function record() {
    if (!$("#recordFrame").is(":visible")) {
        $("#recordFrame").show();
    //$("#recordButton").text("Save/Hide");
    //$("#recordText").html("Copy the link below when your recording is ready <input id='recordLink' />");
    } 
}

function retrieveRecordLink()
{

    var listName  = $.cookie("currentWordList");
    if (listName.length==0) listName= $.trim($('#listname').val());
     
    $.get("retrieveRecordLink.jsp", {
        listName: listName
    }, function (data) {
        $("#userAudio").html("User Audios <br/>" + data);
    });
}

function wordList(listName) {
    $.cookie("currentWordList", listName, {
        expires: 365
    });
    //alert($.cookie("currentWordList"));
    $.get("retrieve.jsp", {
        listName: listName
    },
    function (data){
        $("#wordlist").html(data);
    });
       
    retrieveRecordLink();
}
function getPlayer(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}
fixedsrc="http://www.strangecube.com/audioplay/online/audioplay.swf?file=http://onelook.com/pronounce/macmillan/US/word-American-English-pronunciation.mp3&auto=no&sendstop=yes&repeat=1&buttondir=http://www.strangecube.com/audioplay/online/alpha_buttons/negative_small&bgcolor=0xffffff&mode=playpause&einterface=yes";

function play1(o)
{
               
    getPlayer('test').src = fixedsrc.replace(/word/g, o);
    for(i=0;
        i<100000;
        i++);

    //alert( getPlayer('test').src);
    getPlayer('test').playMusic();
}


function hideAll()
{
    $("iframe").css("display", "none");
    $("a.button").css("color", "#333");
}

function retrieveMyList()
{
     
    $.get("retrieveMyList.jsp", function(data) {
        // alert(data);
        $("#myWordList").html(data);
    }); 
}

function retrieveExisting(ob)
{
    newwords = $.trim($("#upload").val());
    listName = $.trim(ob.value);
    if (listName.length==0) return;
    $.get("retrieveExisting.jsp",{
        listName: listName
    }, function(data) {
        if (data == null || data.length==0 ) return;
        words = data.split(" ");
        for(i=0; i<words.length;i++) {
            if (newwords.indexOf($.trim(words[i]))<0) {
                //alert(newwords);
                //alert("--"+words[i]+"--");
                newwords = data + "\n\n"+ newwords;
                $("#upload").val(newwords);
                break;
            }
                
        }
            
       
    // alert(data);
    //$("#myWordList").html(data);
    });
}


function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}


var iFrequency = 5000; // expressed in miliseconds
var myInterval = 0;

// STARTS and Resets the loop if any
function startLoop() {
    if(myInterval > 0) clearInterval(myInterval);  // stop
    //myInterval = setInterval( " $('#k').attr('src', '/pics/k1.jpg?' + (new Date()).getTime()); ", iFrequency );  // run

}
startLoop();
